<?php

namespace app\service;


class MongoDb
{
    //--------------  定义变量  --------------//
    private static $ins = [];
    private static $def = "default";
    private $_conn = null;
    private $_db = null;
    private static $_config = [
        "default" => ["url" => "mongodb://127.0.0.1:27017", "dbname" => "test"],
    ];

    /**
     * 创建实例
     * @param string $confKey
     * @return \mongodb
     */
    public static function getInstance($confKey = NULL)
    {
        if (!$confKey) {
            $confKey = self::$def;
        }
        if (!isset(self::$ins[$confKey]) && ($conf = self::$_config[$confKey])) {
            self::$ins[$confKey] = new mongodb($conf);
        }
        return self::$ins[$confKey];
    }

    /**
     * 构造方法
     * 单例模式
     */
    private function __construct(array $conf)
    {
        $this->_conn = new \MongoDB\Driver\Manager($conf["url"] . "/{$conf["dbname"]}");
        $this->_db = $conf["dbname"];
    }

    /**
     * 插入数据
     * @param string $collname
     * @param array $documents [["name"=>"values", ...], ...]
     * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array]
     * @return \MongoDB\Driver\Cursor
     */
    function insert($collname, array $documents, array $writeOps = [])
    {
        $cmd = [
            "insert" => $collname,
            "documents" => $documents,
        ];
        $cmd += $writeOps;
        return $this->command($cmd);
    }

    /**
     * 删除数据
     * @param string $collname
     * @param array $deletes [["q"=>query,"limit"=>int], ...]
     * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array]
     * @return \MongoDB\Driver\Cursor
     */
    function del($collname, array $deletes, array $writeOps = [])
    {
        foreach ($deletes as &$_) {
            if (isset($_["q"]) && !$_["q"]) {
                $_["q"] = (object)[];
            }
            if (isset($_["limit"]) && !$_["limit"]) {
                $_["limit"] = 0;
            }
        }
        $cmd = [
            "delete" => $collname,
            "deletes" => $deletes,
        ];
        $cmd += $writeOps;
        return $this->command($cmd);
    }

    /**
     * 更新数据
     * @param string $collname
     * @param array $updates [["q"=>query,"u"=>update,"upsert"=>boolean,"multi"=>boolean], ...]
     * @param array $writeOps ["ordered"=>boolean,"writeConcern"=>array]
     * @return \MongoDB\Driver\Cursor
     */
    function update($collname, array $updates, array $writeOps = [])
    {
        $cmd = [
            "update" => $collname,
            "updates" => $updates,
        ];
        $cmd += $writeOps;
        return $this->command($cmd);
    }

    /**
     * 查询
     * @param string $collname
     * @param array $filter [query]     参数详情请参见文档。
     * @return \MongoDB\Driver\Cursor
     */
    function query($collname, array $filter, array $writeOps = [])
    {
        $cmd = [
            "find" => $collname,
            "filter" => $filter
        ];
        $cmd += $writeOps;
        return $this->command($cmd);
    }

    /**
     * 执行MongoDB命令
     * @param array $param
     * @return \MongoDB\Driver\Cursor
     */
    function command(array $param)
    {
        $cmd = new \MongoDB\Driver\Command($param);
        return $this->_conn->executeCommand($this->_db, $cmd);
    }

    /**
     * 获取当前mongoDB Manager
     * @return MongoDB\Driver\Manager
     */
    function getMongoManager()
    {
        return $this->_conn;
    }
}